"""author: wenyao
   data: 2020/11/9
   project: ludingji
"""

import re

#########查找匹配####################
#1、search  和 match  查找第一个匹配
#search 是接收一个正则表达式和字符串，并返回发现的第一个匹配。
#match 从字符串头查找匹配
#只匹配字符串的开始

# >>> re.search("sanchuang","hello world, this is sanchuang")
# <_sre.SRE_Match object; span=(21, 30), match='sanchuang'>
# >>> re.search("sanchuang","hello world, this is")
# >>> result = re.search("sanchuang","hello world, this is")
# >>> print(result)
# None
# >>> re.match("sanchuang","hello world, this is sanchuang")
# >>> re.match("sanchuang","sanchuang hello world, this is ")  #只匹配字符串的开始
# <_sre.SRE_Match object; span=(0, 9), match='sanchuang'>

#  r'sanle' 中的r代表的是raw（原始字符串）
# 原始字符串与正常字符串的区别是原始字符串不会将\字符解释成一个转义字符
# 正则表达式使用原始字符很常见且有用
# >>> re.search(r"\\\\tsanle","hello\\\\tsanle")
# <_sre.SRE_Match object; span=(5, 13), match='\\\\tsanle'>
# >>> re.search("\\\\tsanle","hello\\\\tsanle")
# <_sre.SRE_Match object; span=(6, 13), match='\\tsanle'>
# >>> re.search("\\tsanle","hello\\\tsanle")
# <_sre.SRE_Match object; span=(6, 12), match='\tsanle'>

##match对象
# msg = "It's raining cats and dogs， cats1 cats2"
# match = re.search(r"cats",msg)
# print(match.group())
# print(match.start())
# print(match.end())
# print(match.groups())

#findall 和finditer
# msg = "It's raining cats and dogs， cats1 cats2"
# result = re.findall("cats",msg)
# print(result)
# result2 = re.finditer("cats",msg)
# # for i in result2:
# #     print(i.group())
# print(list(result2))

# re.sub('匹配正则','替换内容','string')
# msg = "I am learning python python2 python3"
# print(re.sub("python","PYTHON",msg))

#compile
# msg = "I am learning python"
# msg2 = "I am learning English"
# msg3 = "hello world"
# reg = re.compile("python")
# print(reg.findall(msg))
# print(reg.findall(msg2))
# print(reg.findall(msg3))
#print(re.findall("python",msg))
#print(re.findall("python",msg2))
#print(re.findall("python",msg3))


##########正则表达式##########
#区间  可根据ascii码的范围来取字符
# ret = re.findall("Python","Python 3 python")
# print(ret)
# ret = re.findall("[Ppfg]ython","Python 3 python fython Fython")
# print(ret)
# ret = re.findall("[a-zA-Z\-]","abcABC123-")
# print(ret)

#匹配或
# msg = "It's raining cats and dogs"
# ret = re.search("cats|dogs",msg)
# print(ret.group())
# ret = re.findall("cats|dogs", msg)
# print(ret)

#区间取反
# ret = re.search("a[^a-z]","1ab3cb3a1a#!aB")
# print(ret)
# ret = re.findall("a[^a-z][^a-z]","1ab3cb3a1a#!aB")
# print(ret)

#[0-z] --> [0-z] ascii码的区间范围
# ret = re.findall("[0-z]","1ab3cb3a1a#>=?!aB")
# print(ret)
# ret = re.findall("[^0-9A-Za-z]","1ab3cb3a1a#>=?!aB")
# print(ret)

#. 占位符，表示任意一个字符（除\n之外）
# ret = re.findall("p.thon","Python pYthon python pthon p-thon")
# print(ret)
# ret = re.findall("p.thon","Python pYthon python pthon p thon")
# print(ret)

#快捷方式

# 快捷标识	功能
# \A	匹配字符串开始
# \bword\b	词边界
# \w	匹配包括下划线的任何单词字符。等价于'[A-Za-z0-9_]'
# \W	匹配任何非单词字符。等价于 '[^A-Za-z0-9_]'
# \d	匹配一个数字字符。等价于 [0-9]
# \D	匹配一个非数字字符。等价于 [^0-9]
# \s	匹配任何空白字符，包括空格、制表符、换页符等等。等价于 [ \f\n\r\t\v]
# \S	匹配任何非空白字符。等价于 [^ \f\n\r\t\v]

#数字、字母、下划线 不算做边界
#\b  词边界  单词边界
#\B
# ret = re.finditer(r"world\b","hello world world123 123world =world ##world## abcworldabc")
# print(list(ret))
# ret = re.finditer(r"\bworld\b","hello _world world123 123world =world ##world## abcworldabc")
# print(list(ret))
# ret = re.finditer(r"\Bworld\B","hello _world world123 123world =world ##world## abcworldabc")
# print(list(ret))
#
#\w \W
# ret = re.findall(r'\w',"python3#")
# print(ret)
# ret = re.findall(r'\W',"python3#")
# print(ret)

#  开始与结束：^, $
# 匹配以python开头：^python
# 匹配以python结尾：python$
# ret = re.findall("^python","hello python")
# print(ret)
# ret = re.findall("^python","python123#")
# print(ret)
# ret = re.findall("python$","hello python")
# print(ret)
# ret = re.findall("^python$","hello python")
# print(ret)

##########正则重复
#通配符
#通配符：？，*，+
#? 0次或1次
# ret = re.findall("\w?","python#!3")
# print(ret)
#*  0次到任意次
# ret = re.findall("\w*","python3#")
# print(ret)
#+  一次以上
# ret = re.findall("\w+","python3#")
# print(ret)
#{n, m}  指定匹配n到m次
# ret = re.findall("\w{1,3}","python32#")
# print(ret)
#
# #贪婪模式和非贪婪模式
# msg = "<div>test</div>bb<div>test2</div>"
# ret = re.findall("<div>.*</div>",msg)
# print(ret)
# ret = re.findall("<div>.*?</div>",msg)
# print(ret)

# ######
# wen tel: 17375722991
# wen tel: 173-7572-2992
# wen tel: 173 7572 2993
# wen tel: (+86) 17375722994

#####正则分组#######
# ret = re.search(r'(\d{3})-(\d{3})-(\d{3})',"123-321-234abc")
# print(ret.group())
# print(ret.group(0))
# print(ret.group(1))
# print(ret.group(2))
# print(ret.group(3))
# # print(ret.group(4))
# print(ret.groups())

#捕获分组和非捕获分组
#分组向后引用
# str = "a1 a2 a1 a2"

#捕获分组(分组并捕获，分组之后匹配到的数据，会放在内存中，并且给定一个从1开始的索引)
#print(re.search(r"([a-z]\d ){3}([a-z]\d)", str))
# print(re.search(r"([a-z]\d)\s+([a-z]\d)\s+\1\s+\2",str).group())
# #findall  有捕获分组的话， 只会输出捕获分组里的内容
# print(re.findall(r"([a-z]\d)\s+([a-z]\d)\s+\1\s+\2",str))

#非捕获分组(?:)  只是分组，不捕获，不会在内存中缓存分组匹配的值，不会分配从1开始的下标索引
# str2 = "a1 a2 a2 a1"
# ret = re.search(r"(?:\w\d)\s+(\w\d)\s+\1",str2)
# print(ret)
#findall  有捕获分组的话 只会输出捕获分组里的内容
# ret = re.findall(r"(?:\w\d)\s+(\w\d)\s+",str2)
# print(ret)

# #1、匹配里面的每一个单词，引号部分作为一个单词输出
msg = 'san chuang tong le, huan ying ni "hello world", learn python'
print(re.findall(r'\w+|"[\w ]+"',msg))
print(re.findall(r'\w+|(?<=\")[\w ]+(?=")',msg))
print(re.findall(r'(?<=\")[\w ]+(?=")|\w+',msg))
# #print(re.findall(r'\w+',msg))
# print(re.findall(r'".*"',msg))
# #2、
# msg2 = 'yyy@xx.com123@qq.comcomaaa@126.combbb@163.comcc@abc.com'
# #--->123@qq.com  aaa@126.com bbb@163.com
# #ret2 = re.findall("\w*@\w*.com",msg2)
# ret = re.findall("(?:\.com)?(\w+@(?:qq|163|126).com)",msg2)
# print(ret)

#3、
# msg3 = "<html>test1<div>test2</div2><a>test3</a></html>"
# # reg3 = re.findall(r"(?:<\w+>)\w+(?:</\w+>)",msg3)
# ret = re.findall(r'<(\w+)>(\w+)</\1>',msg3)
# #ret = re.findall(r"<\w+>\w+</\w>", msg3)
# print(ret)
####------>输出test2  test3
#爬虫

#命名分组
# ret = re.search(r'(?P<name>\d{3})-\d{3}-(?P<last>\d{3})',"123-321-234abc")
# print(ret.group(1))
# print(ret.group("name"))
# print(ret.group(2))
# print(ret.group("last"))
# print(ret.groupdict())

#引用分组
# msg = """
# <div>hello</div>
# <h1>hello</h1>
# """
# ret = re.findall(r'(<(\w+)>hello</\2>)',msg)
# print(ret)

#正则标记
msg = """
Python
python
"""
#对大小写不敏感的标志位
# ret = re.findall(r"^python$",'PYTHOn',re.I)
# print(ret)
# ret = re.findall(r"^python$",msg,re.I)
# print(ret)
#
# #多行匹配和大小写不敏感
# ret = re.findall(r"^python$",msg,re.I|re.M)
# print(ret)
#re.S
# ret = re.findall(r".+",msg)
# print(ret)
# ret = re.findall(r".+",msg, re.S)
# print(ret)

#内联标记


#提取url域名
# msg = """
# https://music.163.com/#/discover/toplist
# http://www.baidu.com?a=1
# http://xinlang.cn
# https://news.xin.lang.cn
# htp://www.baidu.com
# http://1...com
# """
# #ret = re.findall('(?:http://|https://)(\w+.\w+.\w+)',msg,re.M)
# #ret = re.findall('(?:https?://)((?:\w+\.)+\w+)',msg,re.M)
# ret = re.findall(r"^(?:https|http)://(\w+\.(?:\w+\.)*(?:com|cn))",msg,re.M)
# print(ret)

#零宽断言
# s = "a regular rbgular expression"
# # s= "a regularression"
# print(re.findall(r"\w+(?=gular)",s))
# print(re.findall(r"re(?!gular)",s))
# print(re.findall(r"(?<=re)gular",s))
# print(re.findall(r"(?<!re)gular",s))
#
# ip = "inet 192.168.0.132/24 brd 192.168.0.255 scope global noprefixroute ens33"
# print(re.findall(r"(?<=inet ).*(?=/)",ip))

email="abc1234@134-abc.com"
print(re.findall(r"\w{6,18}@[0-9A-Za-z\-]+\.com",email))

#msg = "<img src="/uploads/allimg/170817/1-1FQG416040-L.gif" class="imgMiddle" /><img src="/uploads/allimg/170817/1-1FQG416040-L.gif" class="imgMiddle" />"